<?php

/**
 * This is the model class for table "{{segmentprojects}}".
 *
 * The followings are the available columns in table '{{segmentprojects}}':
 * @property integer $id
 * @property string $create_at
 * @property string $modify_at
 * @property string $create_user_id
 * @property string $update_user_id
 * @property integer $unitproject_id
 * @property string $name
 * @property string $description
 * @property integer $quality
 * @property string $quality_details
 * @property integer $safety
 * @property string $safety_details
 * @property string $pre_start_at
 * @property string $pre_end_at
 * @property string $real_start_at
 * @property string $real_end_at
 * @property string $type
 * @property integer $end_work_status
 * @property integer $start_work_status
 * @property integer $is_main
 * @property double $plan_invest
 * @property double $real_invest_used
 * @property string $quality_file
 *
 * The followings are the available model relations:
 * @property Unitprojects $unitproject
 */
class SegmentProject extends CActiveRecord
{
	public $tempFile;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return SegmentProject the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{segmentprojects}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, pre_start_at, pre_end_at, is_main, plan_invest', 'required'),
			array('unitproject_id, quality, safety, end_work_status, is_main', 'numerical', 'integerOnly'=>true),
			array('plan_invest, real_invest_used', 'numerical'),
			array('plan_invest', 'greaterThanZero'),
			array('pre_start_at, pre_end_at', 'overSpecificTime'),
			array('pre_end_at', 'overPreStartTime'),
			array('real_end_at', 'overRealStartTime'),
			array('create_user_id, update_user_id', 'length', 'max'=>10),
			array('name', 'length', 'max'=>48),
			array('tempFile', 'file', 'types'=>'pdf, doc, docx, pages', 'safe'=>true, 'allowEmpty' => true),
			array('description, quality_details, safety_details, type, quality_file', 'length', 'max'=>255),
			array('modify_at, pre_start_at, pre_end_at, real_start_at, real_end_at', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, create_at, modify_at, create_user_id, update_user_id, unitproject_id, 
					name, description, quality, quality_details, safety, safety_details, 
					pre_start_at, pre_end_at, real_start_at, real_end_at, type, 
					end_work_status, start_work_status, is_main, plan_invest, real_invest_used, quality_file', 'safe', 'on'=>'search'),
		);
	}
	
	public function overSpecificTime($attribute,$params)
	{
		if (strtotime($this->$attribute)<=strtotime('1980-1-1'))
			$this->addError($attribute, $this->attributeLabels()[$attribute] . Yii::t('common',' has to be greater than Jan. 1st, 1980'));
	}
	
	public function overPreStartTime($attribute,$params)
	{
		if (strtotime($this->$attribute)<=strtotime($this->pre_start_at))
			$this->addError($attribute, $this->attributeLabels()[$attribute] . Yii::t('project',' has to be greater than Pre Start Time'));
	}
	
	public function overRealStartTime($attribute,$params)
	{
		if ($this->end_work_status == 1 && strtotime($this->$attribute)<=strtotime($this->real_start_at)) {
			$this->addError($attribute, $this->attributeLabels()[$attribute] . Yii::t('project',' has to be greater than Real Start Time'));
		}
	}
	
	public function greaterThanZero($attribute,$params)
	{	
		if ($this->$attribute<=0)
			$this->addError($attribute, $this->attributeLabels()[$attribute] . Yii::t('common',' has to be greater than 0'));
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'unitproject' => array(self::BELONGS_TO, 'UnitProject', 'unitproject_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'create_at' => Yii::t('common','Create At'),
			'modify_at' => Yii::t('common','Modify At'),
			'is_main' => Yii::t('project','Is Main'),
			'create_user_id' => 'Create User',
			'update_user_id' => 'Update User',
			'unitproject_id' => Yii::t('project','Belonging Unit Project'),
			'name' => Yii::t('project','Segment Project Name'),
			'description' => Yii::t('project','Segment Project Description'),
			'quality' => Yii::t('project','Segment Project Quality'),
			'quality_details' => Yii::t('project','Segment Project Quality Details'),
			'safety' => Yii::t('project','Segment Project Safety'),
			'safety_details' => Yii::t('project','Segment Project Safety Details'),
			'pre_start_at' => Yii::t('project','Pre Start At'),
			'pre_end_at' => Yii::t('project','Pre End At'),
			'real_start_at' => Yii::t('project','Real Start At'),
			'real_end_at' => Yii::t('project','Real End At'),
			'type' => Yii::t('project','Segment Project Type'),
			'plan_invest' => Yii::t('project','Plan Invest'),
			'real_invest_used' => Yii::t('project','Real Invest Used'),
			'quality_file' => Yii::t('project','Segment Project Quality File'),
			'tempFile' => Yii::t('project','Segment Project Quality File'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('create_at',$this->create_at,true);
		$criteria->compare('modify_at',$this->modify_at,true);
		$criteria->compare('create_user_id',$this->create_user_id,true);
		$criteria->compare('update_user_id',$this->update_user_id,true);
		$criteria->compare('unitproject_id',$this->unitproject_id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('quality',$this->quality);
		$criteria->compare('quality_details',$this->quality_details,true);
		$criteria->compare('safety',$this->safety);
		$criteria->compare('safety_details',$this->safety_details,true);
		$criteria->compare('pre_start_at',$this->pre_start_at,true);
		$criteria->compare('pre_end_at',$this->pre_end_at,true);
		$criteria->compare('real_start_at',$this->real_start_at,true);
		$criteria->compare('real_end_at',$this->real_end_at,true);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('end_work_status',$this->end_work_status);
		$criteria->compare('start_work_status',$this->start_work_status);
		$criteria->compare('is_main',$this->is_main);
		$criteria->compare('quality_file',$this->quality_file);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	protected function beforeSave()
	{
		if(parent::beforeSave()){
			if($this->isNewRecord){
				$this->create_at = date('Y-m-d H:i:s');
				$this->modify_at = date('Y-m-d H:i:s');
				$this->create_user_id = Yii::app()->user->id;
				$this->update_user_id = Yii::app()->user->id;
			}else{
				$this->modify_at = date('Y-m-d H:i:s');
				$this->update_user_id = Yii::app()->user->id;
			}
			return true;
		}else{
			return false;
		}
	}
}